package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description SetmealServiceImpl
 * @Author liuqiang
 * @Date 2024/2/20
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //新增套餐信息
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);
        //获取新增的套餐id
        Long setmealId = setmeal.getId();
        //取出SetmealDTO中的setmealDishes
        //循环遍历数据跟套餐id一一对应
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //将数据存入setmeal_dish表
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /**
     * 分页查询套餐信息
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //配置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //执行分页SQL
        Page<SetmealVO> list = setmealMapper.listSetmeal(setmealPageQueryDTO);
        //封装数据并返回
        return new PageResult(list.getTotal(),list.getResult());
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //根据id查询套餐信息
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据setmeal的id查询setmeal_dish的setmeal_id,返回List<SetmealDish>
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    /**
     * 修改套餐及对应套餐菜品关系表信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐信息
        setmealMapper.updateSetmeal(setmeal);
        //删除套餐菜品关系中关联的旧信息
        setmealDishMapper.deleteSetmealDishBysSetmealId(setmealDTO.getId());
        //新增新的信息
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            //向口味表插入n条数据
            setmealDishMapper.insertSetmealDish(setmealDishList);
        }
    }

    /**
     * 批量删除套餐信息
     * @param ids
     */
    @Transactional
    @Override
    public void deleteSetmealList(List<Long> ids) {
        //判断套餐是否是在
        for (Long id : ids){
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            //在售不能删除
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐表中套餐信息
        setmealMapper.deleteSetmealByIds(ids);
        //删除套餐和菜品关联表中的信息
        setmealDishMapper.deleteSetmealDishBysSetmealIds(ids);
    }

    /**
     * 套餐起售、停售
     * @param status
     */
    @Override
    public void updateSetmealStatus(Integer status,Long id) {
        //套餐内如果有停售菜品，则套餐无法上架
        //通过id查询setmeal_dish得到List<dishId>
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealDishBySetmealId(id);
        //遍历
        setmealDishList.forEach(setmealDish -> {
            //根据dishId查询setmeal数据，如果有停售则抛出异常
            Dish dish = dishMapper.getDishById(setmealDish.getDishId());
            if (dish.getStatus() == StatusConstant.DISABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.getSetmealList(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
